package org.ru.mse10.cvis.web.bean.action.search;

import org.ru.mse10.cvis.dto.BaseDTO;
import org.ru.mse10.cvis.entity.misc.Identity;
import org.ru.mse10.cvis.service.EntityService;
import org.ru.mse10.cvis.web.bean.action.search.datamodel.JPADataModel;

/**
 * The Class BasesSearchAction.
 * 
 * @param <R>
 *            the generic type
 * @param <E>
 *            the element type
 * @param <T>
 *            the generic type
 */
public abstract class BasesSearchAction<R extends EntityService, E extends Identity, T extends BaseDTO<E>> {

	/** The data model. */
	private JPADataModel<R, E, T> dataModel;

	/** The selected id. */
	private Long selectedId;

	/**
	 * Preview search result.
	 * 
	 * @return the string
	 */
	public abstract String previewSearchResult();

	/**
	 * Action search.
	 * 
	 * @param args
	 *            the args
	 */
	public void actionSearch(T args) {
		setDataModel(createDataModel(args));
		getService().search(getDataModel().getSearcher(), args, true);
	}

	/**
	 * Gets the service.
	 * 
	 * @return the service
	 */
	public abstract R getService();

	/**
	 * Creates the data model.
	 * 
	 * @param dto
	 *            the dto
	 * @return the jPA data model
	 */
	public abstract JPADataModel<R, E, T> createDataModel(T dto);

	/**
	 * Gets the data model.
	 * 
	 * @return the data model
	 */
	public JPADataModel<R, E, T> getDataModel() {
		return dataModel;
	}

	/**
	 * Sets the data model.
	 * 
	 * @param dataModel
	 *            the data model
	 */
	public void setDataModel(JPADataModel<R, E, T> dataModel) {
		this.dataModel = dataModel;
	}

	/**
	 * Gets the selected id.
	 * 
	 * @return the selected id
	 */
	public Long getSelectedId() {
		return selectedId;
	}

	/**
	 * Sets the selected id.
	 * 
	 * @param selectedId
	 *            the new selected id
	 */
	public void setSelectedId(Long selectedId) {
		this.selectedId = selectedId;
	}
}
